package com.ktjy.test2;

import java.lang.reflect.Field;

public class AccessUserFoelds {
    public static void main(String[] args) {
        try{
            Class clz = Class.forName("com.ktjy.mapper.User");
            Object  obj = clz.newInstance(); //创建User对象
            //获取属性 String name String password
            Field name=clz.getDeclaredField("name");
            Field password=clz.getDeclaredField("password");
            //解锁
            name.setAccessible(true);
            password.setAccessible(true);
            System.out.println("赋值前:"+"name:"+name.get(obj)+"  password:"+password.get(obj));
            //赋值
            name.set(obj,"马嘉祺");
            password.set(obj,"123456");
            System.out.println("赋值后:"+"name:"+name.get(obj)+"  password:"+password.get(obj));


        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
